/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.hibernate.criteria.field;

import java.util.Map;

import com.genia.toolbox.basics.manager.CollectionManager;
import com.genia.toolbox.persistence.hibernate.bean.impl.QueryableDisplayObject;
import com.genia.toolbox.persistence.hibernate.criteria.HibernateCriteria;
import com.genia.toolbox.persistence.hibernate.criteria.HibernateCriteriaType;

/**
 * this class represent a property of a persistent object, or the object itself.
 */
public class HibernatePropertyField
    implements HibernateField
{

  /**
   * the <code>CriteriaType</code> of the object manipulated, or null if the
   * object is the main object of the query.
   */
  private final transient HibernateCriteriaType<?> type;

  /**
   * the property to extract, or null if the object is to be retrieved.
   */
  private final transient String property;



  /**
   * returns an escaped version of the current Field.
   * 
   * @return an escaped version of the current Field
   * @see com.genia.toolbox.persistence.hibernate.criteria.field.HibernateField#escapedField()
   */
  public HibernateField escapedField()
  {
    return this;
  }



  /**
   * constructor.
   * 
   * @param type
   *          the <code>CriteriaType</code> of the object manipulated, or null
   *          if the object is the main object of the query
   * @param property
   *          the property to extract, or null if the object is to be retrieved
   */
  public HibernatePropertyField(final HibernateCriteriaType<?> type, final String property)
  {
    this.type = type;
    this.property = property;
  }



  /**
   * constructor.
   * 
   * @param property
   *          the property to extract, or null if the object is to be retrieved
   */
  public HibernatePropertyField(final String property)
  {
    this(null, property);
  }



  /**
   * constructor.
   * 
   * @param type
   *          the <code>CriteriaType</code> of the object manipulated, or null
   *          if the object is the main object of the query
   */
  public HibernatePropertyField(final HibernateCriteriaType<?> type)
  {
    this(type, null);
  }



  /**
   * returns if this <code>Field</code> is an aggregate.
   * 
   * @return <code>true</code> if this <code>Field</code> is an aggregate,
   *         <code>false</code> otherwise.
   */
  public boolean isAggregate()
  {
    return false;
  }



  /**
   * append to stringBuilder the part of the query represented by this object.
   * 
   * @param canHaveParameter
   *          can this part of the query use named parameters?
   * @param collectionManager
   *          The <code>CollectionManager</code> that allow us to perform
   *          joins
   * @param criteria
   *          the <code>HibernateCriteria</code> the represents the all query
   * @param stringBuilder
   *          the <code>StringBuilder</code> to append to.
   * @param queryableDisplayObject
   *          the <code>DisplayObject</code> that allows to convert this
   *          <code>Queryable</code> to a <code>String</code>
   */
  public void appendQuery(final boolean canHaveParameter, final CollectionManager collectionManager, final HibernateCriteria<?> criteria, final StringBuilder stringBuilder,
      final QueryableDisplayObject queryableDisplayObject)
  {
    final HibernateCriteriaType<?> type = (this.type == null ? criteria.getBaseMainType() : this.type);
    stringBuilder.append(type.getAlias());
    if (property != null) {
      stringBuilder.append(".");
      stringBuilder.append(property);
    }
  }



  /**
   * Fill the parameters <code>Map</code> with the constants elements of this
   * query.
   * 
   * @param parameters
   *          the <code>Map</code> to be filled
   */
  public void fillParameters(final Map<String, Object> parameters)
  {
    // Do nothing
  }
}
